"use client";
import { useReadContract,useBlockNumber  } from 'wagmi';
import vaultAbi from '../abi/vaultAbi.json';
import MiniVault_ABI from '../abis/MiniVault.json';
import contractAddresses from '../constants/contracts.json'; // 自动生成的地址
import { useAccount } from 'wagmi';
import { useEffect } from 'react';

const VAULT_ADDRESS = contractAddresses.MiniVault  as `0x${string}`;

export function VaultStats() {
  const { isConnected } = useAccount();
  const { data: blockNumber } = useBlockNumber({ watch: true });

  const { data: totalAssets,
      refetch: refetchAssets,
   } = useReadContract({
    address: VAULT_ADDRESS,
    abi: MiniVault_ABI.abi,
    functionName: 'totalAssets',
    query: {
      enabled: isConnected,
    },
  });

  const { data: totalShares ,
      refetch: refetchShares,
    } = useReadContract({
    address: VAULT_ADDRESS,
    abi: MiniVault_ABI.abi,
    functionName: 'totalShares',
    query: {
      enabled: isConnected,
    },
  });

  // 每个新区块手动刷新
  useEffect(() => {
    if (isConnected && blockNumber) {
      refetchAssets();
      refetchShares();
    }
  }, [blockNumber, isConnected]);
  return (
    <div className="text-center text-gray-700 space-y-1 text-sm font-light">
      <p>Total Assets: {isConnected ? BigInt(totalAssets as string || 0).toString() : 0}</p>
      <p>Total Shares: {isConnected ? BigInt(totalShares as string || 0).toString() : 0}</p>
    </div>
  );
}